//
//  WProDetailViewController.swift
//  iiatn
//
//  Created by mnine on 18/3/31.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD
import WebKit

class WProDetailViewController: itBaseViewController, WKNavigationDelegate, UIPopoverPresentationControllerDelegate{

    var proItem : WPro?
    
    private var webview = WKWebView()
    
    @IBOutlet weak var prodtitle: UILabel!
    
    @IBOutlet weak var proddidian: UILabel!
    
    @IBOutlet weak var prodhangye: UILabel!
    
    @IBOutlet weak var prodclickamount: UILabel!
    
    @IBOutlet weak var prodate: UILabel!
    
    @IBOutlet weak var prodshoucang: UIButton!
    
    @IBOutlet weak var prodqxshoucang: UIButton!
    
    @IBOutlet weak var prodcontent: UIWebView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setPlace()
        // 用户id的获取！！！
        prodtitle.text = proItem!.title
        proddidian.text = "地点:\(proItem!.filterattr_value!.components(separatedBy: "|")[0])"
        prodhangye.text = "行业:\(proItem!.filterattr_value!.components(separatedBy: "|")[1])"
        prodclickamount.text = "阅读量:\(proItem!.click_num!)"+"    "+"金额:\(proItem!.amount_money!)万美元"
        prodate.text = "发布日期：\(proItem!.created_at!)"
        setweb(mbody: (proItem?.content)!)

        prodshoucang.addTarget(self, action: #selector(sc), for: .touchUpInside)
        prodqxshoucang.addTarget(self, action: #selector(qxsc), for: .touchUpInside)
    }

    override func setupNav() {
        view.backgroundColor = WColor(r: 250, g: 250, b: 250, a: 0.7)
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "GiftShare_icon_18x22_"), style: .plain, target: self, action: #selector(pop))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setPlace(){
        prodtitle.width = SCREENW - prodtitle.x*2
        prodcontent.width = SCREENW - prodcontent.x*2
        prodcontent.height = SCREENH - prodcontent.y
        
        prodshoucang.layer.cornerRadius = prodshoucang.height * 0.1
        prodqxshoucang.layer.cornerRadius = prodqxshoucang.height * 0.1
    }

    func sc(){
        WNetwork.shareNetworkTool.shoucangPro(user_id: wuserid!, pro_id: proItem!.id!)
    }
    func qxsc(){
        WNetwork.shareNetworkTool.qxshoucangPro(user_id: wuserid!, pro_id: proItem!.id!)
    }
    
    // MARK: - Navigation
    func setweb(mbody: String){
        //创建wkwebview
        SVProgressHUD.showProgress(0.75)
        webview = WKWebView(frame: CGRect(x: prodcontent.x, y: prodcontent.y, width: prodcontent.width, height: prodcontent.height))
        webview.navigationDelegate = self
        var html = "<html>"
        html += "<head>"
        html += "<meta name=\"viewport\"content=\"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no,minimal-ui\">"
        html += "<style>img{max-width:" + String(describing: SCREENW-16) + "px !important;}</style>"
        html += "<body>"
        html += mbody
        html += "</body>"
        html += "</head>"
        html += "</html>"
        webview.loadHTMLString(html, baseURL: nil)
        view.addSubview(webview)
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        SVProgressHUD.dismiss()
    }
    func pop(){
        print("\(proItem?.title!)")
        let popVC = PopViewController()
        popVC.pro = proItem
        popVC.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
        popVC.popoverPresentationController?.sourceView = self.view
        popVC.popoverPresentationController?.delegate = self
        popVC.popoverPresentationController?.backgroundColor = UIColor.white
        present(popVC, animated: true, completion: nil)
    }
    
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
    
    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return true
    }
    
}

